/*
	1. 在执行Promise类的时候，传递的执行器function会被立即执行
	2. Promise中有三种状态，pending/fulfilled/rejected 一旦状态确定就不可更改
	3. 执行器function的参数resolve和reject函数是用来更改状态的
 */
const MyPromise = require('./MyPromise')

let promise = new MyPromise(function(resolve, reject) {
	// throw new Error('executor error')
	resolve('成功')
	// reject(new Error('promise rejected'))
	// setTimeout(function() {
	// 	resolve('成功')
	// }, 1000)
})

// promise.then(value => {
// 	console.log('resolved', value)
// 	// throw new Error('then error')
// 	return 100
// }, reason => {
// 	console.log(reason.message)
// })
// .then(value => {
// 	console.log('resolved', value)
// 	// throw new Error('then error')
// }, reason => {
// 	console.log(reason.message)
// })

// promise.then().then().then(value => console.log(value), reason => console.log(reason))

let p1 = new MyPromise(function(resolve, reject) {
	setTimeout(function(){
		resolve('p1')
	}, 1000)
})

let p2 = new MyPromise(function(resolve, reject) {
	// resolve('p2')
	reject('p2 reject')
})

// MyPromise.all(['a', 'b', p1, p2, 'c'])
// 	.then(value => {
// 		console.log(value)
// 	}, reason => {
// 		console.log(reason)
// 	})

p2.finally(() => {
	console.log('finally')
	// return p1
}).then(value => {
	console.log(value)
}).catch(reason => console.log(reason))